local ClientOHOH = _G.ClientOHOH;

local M = ClientOHOH.class.new(script, ClientOHOH.files.BaseObject())
local CallBackManager = ClientOHOH.load.CallBackManager();
local ConnectClass=_G.CommonOHOH.load.Connect();
local TweenHelper = _G.CommonOHOH.load.TweenHelper();
local BtnHelper = ClientOHOH.load.BtnHelper();

local instStr={

    root="ctrl.content.root",--更节点
    equipBtn="ctrl.content.btn_equip",--装备按钮
    unEquippBtn="ctrl.content.btn_unEquip",--卸载
    goldpBtn="ctrl.content.btn_gold",--黄金按钮
    voidpBtn="ctrl.content.btn_void",--虚空按钮
    mergepBtn="ctrl.content.Merge",--合成按钮
    mergeCount="ctrl.content.MergeCount",--合成数量

    petName="ctrl.content.name",--宠物名
    boosValue="ctrl.content.boostValue",--段位文本
    petView="ctrl.content.PetView",--放置宠物

    titleBack="ctrl.content.sizeBack",--  稀有度
    titleLable="ctrl.content.sizeText",--稀有度

    evolutionBack="ctrl.content.titleBack",-- 段位背景
    evolutionLable="ctrl.content.titleText",--段位文本

    star1="ctrl.content.MergeStar1",
    star2="ctrl.content.MergeStar2",
}

function M.new(...)
	return M(...);
end;

function M.ctor(self,ctrl)
    M.super.ctor(self);
    self.ctrl=ctrl
    self.ui=ctrl:InitUiInst2(ctrl.main_inst,instStr)

    CallBackManager:AddRbxConnect(self,self.ui.equipBtn.Activated, function()
        self.ctrl.EquipEvent:Action({self.ActiveCell:GetPetId()})
    end);

    
    CallBackManager:AddRbxConnect(self,self.ui.unEquippBtn.Activated, function()
        self.ctrl.UnEquipEvent:Action({self.ActiveCell:GetPetId()})
    end);

    CallBackManager:AddRbxConnect(self,self.ui.voidpBtn.Activated, function()
        self.ctrl:Hide()
        ClientOHOH.load.UIManager().ui_list.PetVoidGui:Show()
    end);

    CallBackManager:AddRbxConnect(self,self.ui.goldpBtn.Activated, function()
        self.ctrl:Hide()
        local petId=self.ActiveCell:GetPetId()
        local PetGoldGui= ClientOHOH.load.UIManager().ui_list.PetGoldGui
        PetGoldGui:Show()
        PetGoldGui.InitSelectEvent:Action(petId)
    end);
    
    CallBackManager:AddRbxConnect(self,self.ui.mergepBtn.Activated, function()
        self.ctrl.MergeOneEvent:Action(self.ActiveCell:GetPetId())
    end);
    
end;


local tw1 = TweenInfo.new(12, Enum.EasingStyle.Sine,Enum.EasingDirection.Out,-1,false,2);

function M.RoationModel(self,v)
    TweenHelper:PlayTween(self.AniamtionValue, tw1, {
        Value =v
    });
end


function M.dtor(self)
	M.super.dtor(self);
end;

function M.Show(self,cell)
    if  self.ActiveCell and self.ActiveCell==cell then
        return 
    end
    self.ActiveCell=cell
    if self.Model then
        self.Model:Destroy()
        self.Model=nil
    end

    if  self.UpdCon then
        self.UpdCon:Disconnect()
        self.UpdCon=nil
    end

   

    self.Model=self.ActiveCell:GetPetModel():Clone()
    self.AniamtionValue= Instance.new("IntValue",self.Model)
    self.AniamtionValue.Value=0
    self.Model.Parent=self.ui.petView
    self.Model:SetPrimaryPartCFrame(self.ui.petView.CF.Value)
    self.OriginalModelCF=self.Model:GetPrimaryPartCFrame()
    
    self.AniamtionValue.Changed:Connect(function(value)
        -- local cf=self.Model:GetPrimaryPartCFrame()*CFrame.Angles(0,math.rad(2),0)
        local cf=self.OriginalModelCF*CFrame.Angles(0,math.rad(value),0)
        self.Model:SetPrimaryPartCFrame(cf)
    end)

    self:RoationModel(360)

    self:FullData()

    self.UpdCon= self.ActiveCell.UpdateEvent:Connect(function()
        self:FullData()
    end)
end

function M.FullData(self)

    self.ui.petName.Text=self.ActiveCell:GetPetName()
    self.ui.boosValue.Text=self.ActiveCell:GetBoostStr()
    -- self:FullText(self.boosValue,{self.ActiveCell:GetBoostStr()})

    self.ui.titleLable.Text=self.ActiveCell:GetTitleName()
    self.ui.titleBack.BackgroundColor3=self.ActiveCell:GetTitleColor3()

    self.ui.evolutionLable.Text=self.ActiveCell:GetEvolutionName()
    self.ui.evolutionBack.BackgroundColor3=self.ActiveCell:GetEvolutionColor3()

    self.ui.equipBtn.Visible=false
    self.ui.unEquippBtn.Visible=false
    self.ui.goldpBtn.Visible=false
    self.ui.voidpBtn.Visible=false
    self.ui.mergepBtn.Visible=false

    if self.ActiveCell:IsCanEquip() then
        self.ui.unEquippBtn.Visible=true
    else
        self.ui.equipBtn.Visible=true
    end
    if self.ActiveCell:IsCanMerge() then
        self.ui.mergepBtn.Visible=true
        self:FullMergeLable()
    elseif  self.ActiveCell:IsCanGold()  then
        self.ui.goldpBtn.Visible=true
    elseif  self.ActiveCell:IsCanVoid()  then
        self.ui.voidpBtn.Visible=true
    end

end


function M.FullMergeLable(self)

    local size=self.ActiveCell:GetSizeId()
    self.ui.star1.Visible=false
    self.ui.star2.Visible=false

    if size==1 then
        self.ui.star1.Visible=true
    elseif size==2 then
        self.ui.star1.Visible=true
        self.ui.star2.Visible=true
    end

    local count=self.ctrl:GetDataBase():GetData("pets").had[self.ActiveCell:GetPetId()]
    self.ctrl:FullText(self.ui.mergeCount,{count.."/3"})

end


function M.Hide(self)

end


return M